<template>
    <!-- <el-dialog title="授权" :visible.sync="authorizedialogVisible" :modal-append-to-body="false"> -->
         <div>
        <p>应用：{{obj.appName}} </p>
       <!-- <el-form :model="authorizeForm" label-width="120px" >
        <el-tabs type="border-card" v-model="activeName" @tab-click="handleClick" value="first">
        <el-tab-pane label="用户授权" name="first">
            <el-row class="tac">
            <el-col  >
                <el-button plain @click="authorizeToUser ">授权</el-button>
                <el-form :model="userTab" ref="userTab"  >
                <el-table :data="userTableData" highlight-current-row v-loading="userListLoading" style="width: 100%"  >
                <el-table-column prop="appCode"  label="应用名称">
                </el-table-column>
                <el-table-column prop="loginAccount"  label="登录账号">
                </el-table-column> -->
                <!-- <el-table-column prop="badgeNumber" label="警号">
                </el-table-column>
                <el-table-column prop="tel" label="电话号码">
                </el-table-column> -->
                <!-- <el-table-column prop="createDate" label="授权时间" :formatter="formatCreateDate">
                </el-table-column>
                <el-table-column fixed="right" label="操作" width="90">
                <template slot-scope="scope">
                    <el-button @click="apiDelete(scope.$index, scope.row)" type="text" size="small">删除</el-button>
                </template>
                </el-table-column>
                </el-table>
                </el-form>
            </el-col>             
            <el-col :span="24" class="toolbar">
                <el-pagination layout="total, prev, pager, next, jumper" @current-change="handleCurrentAPiPageChange"
                                :current-page="apiPageNum" :page-size="pageSize" :total="apiTotal">
                </el-pagination>
            </el-col> -->
            <!-- </el-row>
        </el-tab-pane>   -->
        <!-- <el-tab-pane label="部门授权" name="first"> -->
            <el-row class="tac">
            <el-col>
                <!-- <el-button plain @click="authorizeToDept">授权</el-button> -->
                <el-form :model="deptTab" ref="deptTab"  >
                <el-table :data="deptTableData" highlight-current-row v-loading="deptListLoading" style="width: 100%"  >
                    <!-- <el-table-column prop="obj.appCode"  label="应用名称">
                    </el-table-column> -->
                    <el-table-column prop="name"  label="单位名称">
                    </el-table-column>
                    <el-table-column prop="address" label="办公地址">
                    </el-table-column>
                    <el-table-column prop="aa" label="AA">
                    </el-table-column>
                    <!-- <el-table-column prop="a" label="职务类型">
                    </el-table-column>
                    <el-table-column prop="status" label="状态">
                    </el-table-column>  -->
                    <!-- <el-table-column prop="createDate" label="授权时间" :formatter="formatCreateDate">
                    </el-table-column> -->
                    <el-table-column fixed="right" label="操作" width="120">
                    <template slot-scope="scope">
                        <p>姓名:{{scope.row.aa}}</p>
                        <!-- <template > -->
                            <!-- <el-button :formatter="formatAuth" type="text" size="small"></el-button> -->
                            <el-button  v-show="true" @click="Click(scope.row)" type="text" size="small">授权</el-button>
                        <!-- </template> -->
                        <!-- <template  > -->
                           <!-- <el-button v-else-if="scope.row.aa==='undefined'" @click="Click(scope.row)"  type="text" size="small">undefined</el-button> -->
                           <el-button v-show="true" @click="Click(scope.row)"  type="text" size="small">取消授权{{scope.row.address}}</el-button>
                        <!-- </template> -->
                        
                        
                    </template>
                    </el-table-column>
                </el-table>
                </el-form>
            </el-col>             
            <el-col :span="24" class="toolbar">
                <el-pagination layout="total, prev, pager, next, jumper" @current-change="handleCurrentPageChange"
                                :current-page="pageNum" :page-size="pageSize" :total="total">
                </el-pagination>
            </el-col>
            </el-row>
        <!-- </el-tab-pane> -->
        <!-- </el-tabs>
        </el-form> -->
        <!-- <el-dialog title="用户授权" :visible.sync="authorizeUserDialogVisible" :modal-append-to-body="false" width="30%" center>
            <el-tree @check-change="toAuthUser"  node-key="uesrId" ref="tree" :load="loadData" :props="defaultProps" lazy  show-checkbox></el-tree>
              <div  slot="footer" class="dialog-footer">
                <el-button @click="authorizeUserDialogVisible = false">关 闭</el-button>
                <el-button type="primary" @click="AddAuthorizeUser" >添加</el-button>
              </div >
        </el-dialog>
        <el-dialog title="部门授权" :visible.sync="authorizeDeptrDialogVisible" :modal-append-to-body="false" width="30%" center>
            <el-tree  node-key="loginAccount" ref="tree" :load="loadData" :props="defaultProps" lazy  show-checkbox></el-tree>
              <div  slot="footer" class="dialog-footer">
                <el-button @click="authorizeDeptrDialogVisible = false">关 闭</el-button>
                <el-button type="primary" @click="AddAuthorizeDept" >添加</el-button>
              </div >
        </el-dialog> -->

            <!-- <el-tree @check="toAuthUser"  node-key="orgId" ref="tree" :load="loadData" :props="defaultProps" lazy  show-checkbox></el-tree> -->
            <!-- <el-tree @check="toAuthUser" :data="treeData" node-key="orgId" ref="tree" :props="defaultProps"  show-checkbox></el-tree> -->
        <!-- @node-expand="toAuthUser"  -->
        </div>
</template>


<script>
import {authList,authTree,deptTree,addAuth,delAuth,getAuth,wholeTree,getOrganizationList}from '../../../api/api.js';
// const appCode = obj.appCode;
export default {
    props:{
        'obj': {}
    },
    data(){
        return{
            // :
            treeData:[],
            au:'',
            total: 0,
            pageNum: 1,
            pageSize:10,
            activeName: 'first',
            userTableData:[],
            deptTableData:[],
            authorizedialogVisible:false,
            authorizeUserDialogVisible:false,
            authorizeDeptrDialogVisible:false,
            userListLoading:false,
            deptListLoading:false,
            userTab:{
                loginAccount:'',
                badgeNumber:'',
                tel:'',
                createDate:''
            },
            deptTab:{//字段未知
                status:'',
                createDate:''
            },
            authorizeForm:{

            },
            defaultProps: {
            label: 'name',
            children:'organizationVos',
            isLeaf: 'leaf',
            },

        }
    },
    methods:{
        formatCreateDate: function (row, column) {
				return row.createDate = (!row.createDate || row.createDate == '') ? '' : util.formatDate.format(new Date(row.createDate), 'yyyy-MM-dd hh:mm:ss');
            },
        // formatAuth: function (row, column) {
		// 		return row.aa == true ? '取消授权' : row.aa == false ? '授权' : '未知';
        //     },
        Click(row){
            console.log("row。。。。。。。。。。。。。。。。。。。")
            console.log(row)
            console.log(row.aa)
        },
        deptList(){
            let params = {
                pageNum: this.pageNum,
                pageSize:this.pageSize,
            }
            getOrganizationList(params).then(function(result){
                let _this = this;
                // this.deptTableData.push('isAuth')
                // this.deptTableData = result.data.data.list;
                this.pageNum = result.data.data.pageNum;
                this.pageSize = result.data.data.pageSize;
                this.total = result.data.data.totalPage;
                // this.deptListLoading = false;
                this.deptTableData = result.data.data.list.map(function(x,y,z){
                     let params= {
                         appCode:_this.obj.appCode,
                         dept:x.orgSerialNo
                   }
                   getAuth(params).then(function(res){
                       console.log("-----------------------------------------------")
                       console.log(params)
                       debugger
                        if(res.data.length==1){//已授权
                       console.log("----------------------true-------------------------")
                            z[y].aa = true;
                            // return true;
                        }else if(res.data.length==0){//未授权
                       console.log("------------------------false-----------------------")
                            z[y].aa = false;
                        }else{//
                        console.log("--------------------------系统异常-----------------------")
                            // this.$message.warning('系统异常 ');
                        }
                    }.bind(this)).catch(function (error) {
                            console.log(error);
                    })

                });
                _this.deptTableData = result.data.data.list;
                console.log("..............................jj")
                console.log(result.data.data.list);
                console.log(_this.deptTableData);
                console.log("..............................jj")
            }.bind(this)).catch(function (error) {
                this.deptListLoading = false;
                console.log(error);
            }.bind(this));
        },
        // isAuth(args){
        //     let params ={
        //         appCode:this.obj.appCode,
        //         dept:args.orgSerialNo,
        //     }
        //     return getAuth(params).then(function(res){
        //         console.log("getAuth 授权返回。。。。。。。。。。。。。。。。。。。")
        //         console.log(res)
        //         console.log(res.data.length)
        //         if(res.data.length==1){
        //             return true;
        //         }else{
        //             return false;
        //         }
        //     }.bind(this)).catch(function (error) {
        //          console.log(error);
        //     })
        // },
        handleClose(done) {
            this.$confirm('确认关闭？')
            .then(_ => {
                done();
            })
            .catch(_ => {});
        },
        handleClick(tab, event) {
            console.log(tab, event);
        },
        authorizeToUser:function(){
            this.authorizeUserDialogVisible= true;
        },
        authorizeToDept:function(){
            this.authorizeDeptrDialogVisible = true;
        },
        handleCurrentPageChange: function (pageNum) {// 翻页动作
        this.pageNum = pageNum;
        this.deptList();// 翻页后刷新页面数据
        },
        AddAuthorizeUser:function(){
            //todo 保存授权用户
            // debugger;

        },
        // toAuthUser:function(a,b){
        //     //判断动作
            
        //    let data =  b.checkedNodes;
        //    let params ={
        //    }
        //    console.log("-------------------------------------")
        //    console.log(data)
        //    params.toHandleList = data;
        //    params.appCode = this.obj.appCode;
        //    addAuth(params).then(res =>{
        //        console.log(res)
        //    });
        // },
        AddAuthorizeDept:function(){
            //todo
        },
        // loadData(node,resolve){
        //     if(node.level ===0){
        //         let params ={}
        //     authList(params).then(res => {
        //         resolve(res.data);
        //     }).catch(function (error) {
        //         this.$message.warning('系统异常 ');
        //     });
        //     return;
        //     }

        //     if(node.level>0){
        //         let params ={}
        //         params.orgSerialNo = node.data.orgSerialNo;
        //     console.log(params);
        //     deptTree(params).then(res => {
        //         res.data.map(item=>{
        //             // if(item.organization==null){
        //             //     item.leaf=true;
        //             // }
        //         });
        //         resolve(res.data);
        //     }).catch(function (error) {
        //         this.$message.warning('系统异常 ');
        //     });
        //     return;
        //     }
        // return ;
        // },
    },
    mounted(){
        // this.deptList();
        // this.isAuth();
    },
    created(){
    this.deptList();
    }
}

//zteer.map(function(x,y){ x  indexof() intx push()  ,   })    zteer.filter( pop() )
</script>